import 'package:flutter/material.dart';
import 'package:photo_frame/change_password_page.dart';
import 'package:photo_frame/dialog/edit_member_dialog.dart';
import 'package:photo_frame/forget_password_page.dart';
import 'package:photo_frame/import_schedule_page.dart';
import 'package:photo_frame/login_page.dart';
import 'package:photo_frame/member_list_page.dart';
import 'package:photo_frame/pages/benediction/benediction_page.dart';
import 'package:photo_frame/pages/device/device_list_page.dart';
import 'package:photo_frame/pages/gallery/send_asset/send_asset_record_page.dart';
import 'package:photo_frame/pages/gallery/send_asset_preview/send_asset_preview_list_page.dart';
import 'package:photo_frame/pages/gallery/frame_folder/frame_folder_list_page.dart';
import 'package:photo_frame/pages/gallery/gallery_page.dart';
import 'package:photo_frame/pages/me/about_me_page.dart';
import 'package:photo_frame/pages/me/feed_back_page.dart';
import 'package:photo_frame/pages/me/help_me_page.dart';
import 'package:photo_frame/pages/tabbar/tabbar_page.dart';
import 'package:photo_frame/register_page.dart';
import 'package:photo_frame/router/router_context.dart';
import 'package:photo_frame/scan_page.dart';
import 'package:photo_frame/select_frame_page.dart';
import 'package:photo_frame/splash_page.dart';
import 'package:photo_frame/user/user_manager.dart';
import 'package:photo_frame/verification_code_page.dart';
import 'package:photo_frame/widgets/app_webview.dart';

import '../pages/calendar/calendar_page.dart';

typedef PageBuild = Widget Function(Map<String, dynamic>?);

Map<String, PageBuild> _routerMapping = {};
Map<String, PageBuild> routerHooks = {};

PageBuild getPageBuild(String path) {
  print("getPageBuild :${path}");

  return _routerMapping[path] ??
      (args) {
        return Center(child: Text("建设中"));
      };
}

bool isInit = false;

void register() {
  _routerMapping["/"] = (args) => const SplashPage();
  _routerMapping["/login"] = (args) => const LoginPage();
  _routerMapping["/register"] = (args) => const RegisterPage();
  _routerMapping["/changePassword"] = (args) => ChangePasswordPage(args);
  _routerMapping["/main"] = (args) => TabbarPage();
  _routerMapping["/verificationCode"] = (args) => VerificationCodePage(args);
  _routerMapping["/forgetPassword"] = (args) => ForgetPasswordPage();
  _routerMapping["/help"] = (args) => HelpMePage();
  _routerMapping["/about"] = (args) => AboutMePage();
  _routerMapping["/web"] = (args) =>
      AppWebviewPage(title: args?["title"] ?? "", url: args?["url"] ?? "");
  _routerMapping["/feedback"] = (args) => FeedBackPage();
  _routerMapping["/gallery"] = (args) => GalleryPage();
  _routerMapping["/benediction"] = (args) => BenedictionPage();
  _routerMapping["/selectFrame"] = (args) => SelectFramePage(args);
  _routerMapping["/scan"] = (args) => ScanPage();
  _routerMapping["/deviceList"] = (args) => DeviceListPage();
  _routerMapping["/calendar"] = (args) => CalendarPage();
  _routerMapping["/memberListPage"] = (args) => MemberListPage(args);
  _routerMapping["/importSchedule"] = (args) => ImportSchedulePagePage(args);
  _routerMapping["/selectFrameFolder"] = (args) => FrameFolderListPage();
  _routerMapping["/sendAssetPreview"] = (args) => SendAssetPreviewListPage();
  _routerMapping["/sendAssetRecord"] = (args) => PopScope(
        canPop: true,
        child: SendAssetRecordPage(),
      );
}


void _init() {
  isInit = true;
  register();
}

RouteFactory? routeGenerator = (RouteSettings settings) {
  print("object route : ${settings.name}");
  if (!isInit) {
    _init();
  }
  return MaterialPageRoute(builder: (BuildContext context) {
    return routerHooks[settings.name]?.call(
            settings.arguments is Map<String, dynamic>
                ? settings.arguments as Map<String, dynamic>
                : null) ??
        getPageBuild(settings.name ?? "").call(
            settings.arguments is Map<String, dynamic>
                ? settings.arguments as Map<String, dynamic>
                : null);
  });
};
